import store from '../index'
import router from '../../router/index'
import {userGroupById} from '@/api/api'
import {Post} from '@/js/HTTP'

export default {
	state: {
		info: ''  // 每次刷新都要通过token请求个人信息来筛选动态路由
	},
	mutations: {
		getInfo (state, token) {
			let par = {}
			var res = Post(document.location.origin+"/v1/user/userGroupById",par,true)
			if (res != null) {
				var jsonInfo = JSON.parse(res)
				if (jsonInfo.code == 200) {
					var data = jsonInfo.data;
					if (data.length > 0){
						state.info = {
							role:data[0].name,
							permissions: data[0].Describe
						}
						sessionStorage.setItem('info', JSON.stringify(store.getters.info));
						store.dispatch('setRole', store.getters.info);
						return
					}
			}
		
		}
		state.info = {
			role: 'ordinary',
			permissions: '普通用户'
		}
		sessionStorage.setItem('info', JSON.stringify(store.getters.info))
		store.dispatch('setRole', store.getters.info)
		return

			
			
	},
	setRole (state, options) {  // 切换角色，测试权限管理
	  state.info = {
		role: options.role,
		permissions: options.permissions
	  }
	  sessionStorage.setItem('info', JSON.stringify(store.getters.info));
	  store.dispatch('newRoutes', options.role)
	  router.addRoutes(store.getters.addRouters)

	}
  },
  actions: {
	getInfo ({commit}, token) {
	  commit('getInfo', token)
	},
	setRole ({commit}, options){
	  // 权限测试
	  commit('setRole', options)
	}
  }
}
